<template>
	<view>
		<!-- 顶部的搜索栏 -->
		<view class="search-wrapper">
			<view class="search-input">
				<icon type="search" size="16" />
				<input v-model.trim="keyword" @confirm="handleSearch" focus placeholder="输入商品名称" />
				<icon type="clear" size="16" v-if="false" />
			</view>
			<button type="warn" @click="handleSearch">搜索</button>
		</view>
		<!-- 商品列表 -->
		<view class="goods-list">
			<navigator class="goods-item" :url="`/pages/goods_detail/goods_detail?goodsId=${item.goods_id}`" v-for="(item, index) in goodsList" :key="item.goods_id">
				<view class="goods-title">
					{{item.goods_name}}
				</view>
			</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword: '',
				goodsList: [],
				timer: null
			}
		},
		watch:{
			keyword(){
				clearTimeout(this.timer)
				if(this.keyword){
					this.timer = setTimeout(() => {
							uni.request({
								url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/qsearch',
								data: {
									query: this.keyword
								},
								success: (res) => {
									this.goodsList = res.data.message
								}
							})
						
					},1000)
				}
			}
		},
		methods: {
			handleSearch(){
				if(this.keyword){
					wx.navigateTo({
						url:'../goods_list/goods_list?keyword='+this.keyword
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.search-wrapper {
		padding: 20rpx;
		display: flex;

		.search-input {
			flex: 1;
			background: #eee;
			height: 61rpx;
			padding: 0 20rpx;
			margin-right: 20rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-radius: 50px;

			input {
				flex: 1;
				padding: 0 5px;
				font-size: 14px;
			}
		}

		button {
			height: 61rpx;
			line-height: 61rpx;
			font-size: 14px;
		}
	}

	.goods-list {
		padding: 0 20rpx;

		.goods-item {
			padding: 30rpx 0;
			border-bottom: 1px #eee solid;
			display: flex;

			.goods-title {
				flex: 1;
				font-size: 14px;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
				align-self: flex-start;
			}
		}
	}
</style>
